package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.MechanicInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.MechanicInfoRepository;
import co.yixiang.modules.shop.service.MechanicInfoService;
import co.yixiang.modules.shop.service.dto.MechanicInfoDto;
import co.yixiang.modules.shop.service.dto.MechanicInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.MechanicInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "mechanicInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MechanicInfoServiceImpl implements MechanicInfoService {

    private final MechanicInfoRepository mechanicInfoRepository;

    private final MechanicInfoMapper mechanicInfoMapper;

    public MechanicInfoServiceImpl(MechanicInfoRepository mechanicInfoRepository, MechanicInfoMapper mechanicInfoMapper) {
        this.mechanicInfoRepository = mechanicInfoRepository;
        this.mechanicInfoMapper = mechanicInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(MechanicInfoQueryCriteria criteria, Pageable pageable){
        Page<MechanicInfo> page = mechanicInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(mechanicInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<MechanicInfoDto> queryAll(MechanicInfoQueryCriteria criteria){
        return mechanicInfoMapper.toDto(mechanicInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public MechanicInfoDto findById(Integer id) {
        MechanicInfo mechanicInfo = mechanicInfoRepository.findById(id).orElseGet(MechanicInfo::new);
        ValidationUtil.isNull(mechanicInfo.getId(),"MechanicInfo","id",id);
        return mechanicInfoMapper.toDto(mechanicInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public MechanicInfoDto create(MechanicInfo resources) {
        return mechanicInfoMapper.toDto(mechanicInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(MechanicInfo resources) {
        MechanicInfo mechanicInfo = mechanicInfoRepository.findById(resources.getId()).orElseGet(MechanicInfo::new);
        ValidationUtil.isNull( mechanicInfo.getId(),"MechanicInfo","id",resources.getId());
        mechanicInfo.copy(resources);
        mechanicInfoRepository.save(mechanicInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            mechanicInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MechanicInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MechanicInfoDto mechanicInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", mechanicInfo.getName());
            map.put("头像", mechanicInfo.getImg());
            map.put("用户id", mechanicInfo.getUserId());
            map.put("品牌", mechanicInfo.getBrandId());
            map.put("类型", mechanicInfo.getCateId());
            map.put("省", mechanicInfo.getProvince());
            map.put("市", mechanicInfo.getCity());
            map.put("区", mechanicInfo.getDistrict());
            map.put("详细地址", mechanicInfo.getDetail());
            map.put("联系电话", mechanicInfo.getPhone());
            map.put("详细信息", mechanicInfo.getMessage());
            map.put("添加时间", mechanicInfo.getCreateTime());
            map.put("阅读次数", mechanicInfo.getCountNumber());
            map.put("是否删除", mechanicInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}